﻿// =================================================================== 
// 模块说明
//====================================================================
// 创建时间：2018-12-15 16:33:43
// 负责人：liucx
// ===================================================================
using System;
using System.Data;
using System.IO;
using System.Configuration;
using log4net.Appender;
using log4net.Core;
using log4net.Layout;
using log4net.Layout.Pattern;
using OF.Component.Model;

namespace LogCenter.LogCustom
{
    #region 入库sql和参数
    public class LoginLogAdoAppender : LoginLogInfoCustomAdoNetAppender
    {
        public LoginLogAdoAppender()
        {
            //System.Data.SqlClient
            Type t = typeof(System.Data.SqlClient.SqlConnection);
            string s = t.Assembly.FullName.ToString();
            //数据连接类型
            this.ConnectionType = $"System.Data.SqlClient.SqlConnection, {s}";
            base.UseTransactions = false;
            this.CommandType = CommandType.Text;
            //sql语句
            this.CommandText = @"INSERT INTO LoginLog			
			(			
			[LoginDate],
			[LoginIP],
			[LoginUser],
			[LoginPwd],
			[LoginSystem],
			[LoginResult],
			[LoiginDesc],
			[LoginTerminal]
			)
			 VALUES
			(			
			@LoginDate,
			@LoginIP,
			@LoginUser,
			@LoginPwd,
			@LoginSystem,
			@LoginResult,
			@LoiginDesc,
			@LoginTerminal
			)
			 SET @LoginLogID=@@IDENTITY
			";
            string parameterName = "";
            parameterName = "LoginLogID";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.Int64, Layout = new Layout2RawLayoutAdapter(new LoginLogLayout("%" + parameterName)) });
            parameterName = "LoginDate";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.DateTime, Layout = new Layout2RawLayoutAdapter(new LoginLogLayout("%" + parameterName)) });
            parameterName = "LoginIP";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.String, Size = 50, Layout = new Layout2RawLayoutAdapter(new LoginLogLayout("%" + parameterName)) });
            parameterName = "LoginUser";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.String, Size = 30, Layout = new Layout2RawLayoutAdapter(new LoginLogLayout("%" + parameterName)) });
            parameterName = "LoginPwd";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.String, Size = 30, Layout = new Layout2RawLayoutAdapter(new LoginLogLayout("%" + parameterName)) });
            parameterName = "LoginSystem";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.Int32, Layout = new Layout2RawLayoutAdapter(new LoginLogLayout("%" + parameterName)) });
            parameterName = "LoginResult";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.Int32, Layout = new Layout2RawLayoutAdapter(new LoginLogLayout("%" + parameterName)) });
            parameterName = "LoiginDesc";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.String, Size = 500, Layout = new Layout2RawLayoutAdapter(new LoginLogLayout("%" + parameterName)) });
            parameterName = "LoginTerminal";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.String, Size = 50, Layout = new Layout2RawLayoutAdapter(new LoginLogLayout("%" + parameterName)) });

            base.ActivateOptions();
        }
    }
    #endregion

    #region 数据库连接
    public class LoginLogInfoCustomAdoNetAppender : AdoNetAppender
    {
        /// <summary>
        /// 数据库连接
        /// </summary>
        public new string ConnectionStringName
        {
            set
            {
                this.ConnectionString = ConfigurationManager.ConnectionStrings[value].ConnectionString;
            }
        }
    }
    #endregion

    #region 字段转换
    public class LoginLogLayout : PatternLayout
    {
        public LoginLogLayout(string pattern)
            : base(pattern)
        {

        }
        public override void ActivateOptions()
        {

            this.AddConverter("LoginLogID", typeof(LoginLogLoginLogIDConverter));
            this.AddConverter("LoginDate", typeof(LoginLogLoginDateConverter));
            this.AddConverter("LoginIP", typeof(LoginLogLoginIPConverter));
            this.AddConverter("LoginUser", typeof(LoginLogLoginUserConverter));
            this.AddConverter("LoginPwd", typeof(LoginLogLoginPwdConverter));
            this.AddConverter("LoginSystem", typeof(LoginLogLoginSystemConverter));
            this.AddConverter("LoginResult", typeof(LoginLogLoginResultConverter));
            this.AddConverter("LoiginDesc", typeof(LoginLogLoiginDescConverter));
            this.AddConverter("LoginTerminal", typeof(LoginLogLoginTerminalConverter));
            base.ActivateOptions();
        }
    }
    internal sealed class LoginLogLoginLogIDConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            LoginLogInfo log = (LoginLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.LoginLogID);
        }
    }
    internal sealed class LoginLogLoginDateConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            LoginLogInfo log = (LoginLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.LoginDate);
        }
    }
    internal sealed class LoginLogLoginIPConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            LoginLogInfo log = (LoginLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.LoginIP);
        }
    }
    internal sealed class LoginLogLoginUserConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            LoginLogInfo log = (LoginLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.LoginUser);
        }
    }
    internal sealed class LoginLogLoginPwdConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            LoginLogInfo log = (LoginLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.LoginPwd);
        }
    }
    internal sealed class LoginLogLoginSystemConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            LoginLogInfo log = (LoginLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.LoginSystem);
        }
    }
    internal sealed class LoginLogLoginResultConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            LoginLogInfo log = (LoginLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.LoginResult);
        }
    }
    internal sealed class LoginLogLoiginDescConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            LoginLogInfo log = (LoginLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.LoiginDesc);
        }
    }
    internal sealed class LoginLogLoginTerminalConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            LoginLogInfo log = (LoginLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.LoginTerminal);
        }
    }
    #endregion

}